package com.hdu.utils;

import com.hdu.config.DelayTaskConfig;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;


@Component
public class ConnectionHelper {


    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://%s:%s/%s?serverTimezone=UTC";

    private final DelayTaskConfig properties;

    public ConnectionHelper(DelayTaskConfig properties) {
        this.properties = properties;
    }

    public Connection getConnection() {
        try {
            Class.forName(DRIVER_NAME);
            String url = String.format(URL, properties.getDbIp(), properties.getDbPort(), properties.getDbName());
            String user = properties.getDbUserName();
            String password = properties.getDbPassword();
            return java.sql.DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException | SQLException e) {
            return null;
        }
    }
}
